#!coding:utf-8
'''
Created on Mar 6, 2013

@author: join
'''
import os
import shutil
import ConfigParser
import zipfile

cur_dir = os.path.dirname(os.path.abspath(__file__))

def _build():
    os.system("python setup.py build")
    new_version = raw_input("please input your new version,if not a new version,please input word no:\n")
    _copy_files()
    if new_version == 'no':
        pass
    else:
        print 'modify version item in lottery.conf.'
        c = ConfigParser.ConfigParser()
        try:
            CONF_PATH = os.path.join(cur_dir,'build','exe.win32-2.7','lottery.conf')
            c.read(CONF_PATH)
            c.set('default', 'version', new_version)
            c.set('default', 'fullscreen', 'true')
            c.set('default', 'hidemouse', 'true')
            c.set('default', 'coin_port', 'com1')
            c.set('default', 'posboard_port', 'com5')
            c.set('default', 'printer_port', 'com4')
            c.write(open(CONF_PATH, "w"))
        except Exception as e:
            print "can not update config : lottery.conf %s"%e
    replace_yeepaydata = raw_input('do you want to replace yeepaydata,input y or n:\n')
    if replace_yeepaydata == 'y':
        c = ConfigParser.ConfigParser()
        try:
            CONF_PATH = os.path.join(cur_dir,'build','exe.win32-2.7','yeepaydata.conf')
            c.read(CONF_PATH)
            c.set('default', 'pos_serial_number', '100001')
            c.set('default', 'pos_batch_number', '000001')
            c.write(open(CONF_PATH, "w"))
        except Exception as e:
            print "can not update config : yeepaydata.conf %s"%e
        
    _zip_files()

def _copy_files():
    dll_path = os.path.join(cur_dir,'KMY350X.dll')
    lottery_conf_path = os.path.join(cur_dir,'lottery.conf')
    yeepaydata_conf_path = os.path.join(cur_dir,'yeepaydata.conf')
    dest_path = os.path.join(cur_dir,'build','exe.win32-2.7')
    if os.path.exists(dest_path):
        shutil.copy2(dll_path, dest_path)
        shutil.copy2(lottery_conf_path, dest_path)
        shutil.copy2(yeepaydata_conf_path, dest_path)
        
def _zip_files():
    dest_path = os.path.join(cur_dir,'build','exe.win32-2.7')
    filename = os.path.join(cur_dir,'build','lotteryClient.zip')
    zf = zipfile.ZipFile(filename,'w')
    
    for f in os.listdir(dest_path):
        zf.write(dest_path+os.sep+f)
    zf.close()
    print '==================== build finished.====================='
    
_build()
